#!/usr/bin/env bash
MACHINE_NAME=$(printf "%s\n" ${DEBIAN_FOLDER} | sed 's@_@-@g') #此处并非重复操作
unset TMOE_SD_DIR
TMOE_DOCKER_DIR="/media/docker"
if egrep -q '^root:(\*|\!):' ${DEBIAN_CHROOT}/etc/shadow; then
    sed -i -E 's@(^root):.:@\1::@g' ${DEBIAN_CHROOT}/etc/shadow
fi
case $(${TMOE_PREFIX} cat ${DEBIAN_CHROOT}/etc/hostname) in
localhost | LXC_NAME | "")
    ${TMOE_PREFIX} printf "%s\n" "${MACHINE_NAME}" >${DEBIAN_CHROOT}/etc/hostname
    ${TMOE_PREFIX} printf "%s\n" "127.0.0.1       ${MACHINE_NAME}" >>${DEBIAN_CHROOT}/etc/hosts
    ;;
esac
if [[ -s "${DEBIAN_CHROOT}/etc/machine-id" ]]; then
    rm -f ${DEBIAN_CHROOT}/etc/machine-id
fi
set -- "${@}" "--boot"
set -- "${@}" "--directory" "${DEBIAN_CHROOT}"
set -- "${@}" "--uuid" "$(dbus-uuidgen)"
#arch不要设置将其设置为pick
set -- "${@}" "--private-users=no"
set -- "${@}" "--machine" "${MACHINE_NAME}"
set -- "${@}" "--link-journal" "auto"
set -- "${@}" "--resolv-conf" "auto"
set -- "${@}" "--timezone" "auto"
set -- "${@}" "--setenv" "TMOE_CHROOT=true"
set -- "${@}" "--setenv" "TMOE_PROOT=false"
set -- "${@}" "--setenv" "TMOE_DOCKER=false"
#set -- "${@}" "--setenv" '"PULSE_SERVER=unix:/usr/lib/systemd/user/pulseaudio.socket"'
set -- "${@}" "--setenv" "PULSE_SERVER=tcp:127.0.0.1:4713"
set -- "${@}" "--setenv" "DISPLAY=:0.0"
set -- "${@}" "--setenv" "GTK_IM_MODULE=fcitx"
set -- "${@}" "--setenv" "QT_IM_MODULE=fcitx"
set -- "${@}" "--setenv" "XMODIFIERS='@im=fcitx'"
set -- "${@}" "--setenv" "SDL_IM_MODULE=fcitx"
set -- "${@}" "--setenv" "CONTAINER_SYSTEMD=true"
set -- "${@}" "--register" "yes"
set -- "${@}" "--notify-ready" "yes"
for i in ${HOME}/Download /root/sd /sdcard /media/sd; do
    [[ ! -d "${i}" ]] || TMOE_SD_DIR=${i}
done
[[ -z ${TMOE_SD_DIR} ]] || set -- "${@}" "--bind" "${TMOE_SD_DIR}:/media/sd"
[[ ! -e "${TMOE_DOCKER_DIR}" ]] || set -- "${@}" "--bind" "${TMOE_DOCKER_DIR}"
set -- "systemd-nspawn" "${@}"
unset LD_PRELOAD
TMOE_NSPAWN_EXEC="exec ${@}"
su -c "${TMOE_NSPAWN_EXEC}"
#exit 0
